---
title: "Youth political interest analysis"
format: html
output-file: null
---

# 0. Configuration

```{r config, warning=FALSE, echo=FALSE, message=FALSE}
# 0.1. Set de working directory
# It must be the same directory as the other files. Change it with setwd() if necessary

# 0.2. Load packages
library(tidyverse)
library(haven)
library(emmeans)
library(Hmisc)
library(purrr)
library(psych)
library(gt)

# 0.3. Import data
df <- readRDS("clean_data.rds")
```

# Figure 2

Interest in politics by age groups within the control group.

```{r Figure_2}
df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "Control") %>% 
  ggstatsplot::ggbetweenstats(
    x = EDAT_3G_30, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 1,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Age group",
    ylab = "Interest in politics (0-10)",
    title = "Control group"
    )

ggsave(
  "figures/Figure 2.png",
  width = 24,
  height = 15,
  units = "cm"
)
```

# Figure 3

Degree of interest in each specific topic.

```{r Figure_3}
# Re-arrange the data in long format and clean labels for plotting
data_long <- df %>% 
  select(PONDERA_ENLINIA, 
         starts_with("INTERES_") & ends_with("_0_10"), 
         -INTERES_POL_0_10) %>% 
  pivot_longer(-PONDERA_ENLINIA,
               names_to = "POLITICAL_TOPIC",
               values_to = "VALUE_0_10") %>% 
  filter(!is.na(VALUE_0_10)) %>% 
  mutate(POLITICAL_TOPIC = case_match(
    POLITICAL_TOPIC,
    "INTERES_ACCES_HAB_0_10" ~ "Housing\naccess",
    "INTERES_PREC_LAB_0_10" ~ "Job\ninsecurity",
    "INTERES_ELECCIONS_0_10" ~ "Elections",
    "INTERES_PARTITS_0_10" ~ "Political parties",
    "INTERES_CANVI_CLI_0_10" ~ "Climate\nChange",
    "INTERES_FEMINISME_0_10" ~ "Feminism",
    "INTERES_LLENGUA_CAT_0_10" ~ "Language",
    "INTERES_REL_CAT_ESP_0_10" ~ "Catalan-Spanish\nrelations",
    "INTERES_FUNC_ECO_0_10" ~ "Economy",
    "INTERES_SEG_CIUTADANA_0_10" ~ "Security"
  )) %>% 
  mutate(POLITICAL_TOPIC = factor(
    POLITICAL_TOPIC,
    levels = c("Housing\naccess",
               "Job\ninsecurity",
               "Elections",
               "Political parties",
               "Climate\nChange",
               "Feminism",
               "Language",
               "Catalan-Spanish\nrelations",
               "Economy",
               "Security")
  )) 

# Compute weighted mean with CI for each topic
aux <- data_long %>% 
  group_by(POLITICAL_TOPIC) %>% 
  summarise(
    mitj = weighted.mean(VALUE_0_10, PONDERA_ENLINIA),
            n = n(),
            CI_inf = mitj - 1.96 * (sqrt(Hmisc::wtd.var(VALUE_0_10, 
                                                        w = PONDERA_ENLINIA)) / sqrt(n)),
            CI_sup = mitj + 1.96 * (sqrt(Hmisc::wtd.var(VALUE_0_10, 
                                                        w = PONDERA_ENLINIA)) / sqrt(n))
  ) %>% 
  mutate(POLITICAL_TOPIC = paste0(POLITICAL_TOPIC,
                                  "\n(N = ", n, ")"))

aux %>% 
  ggplot(aes(x = reorder(POLITICAL_TOPIC, mitj), y = mitj)) +
    geom_point(size = 2,
               color = "#5E94A9") + 
    geom_errorbar(aes(ymin = CI_inf, ymax = CI_sup), 
                  width = 0.2,
                  color = "#5E94A9",
                  linewidth = 1) + 
    theme_minimal() + 
    labs(
    x = "Political topic",
    y = "Interest (0-10)"
    )

ggsave(
  "figures/Figure 3.png",
  width = 24,
  height = 15,
  units = "cm"
)
```

# Table 1

```{r Table_1}
data_long <- df %>% 
  # Select age and different interest variables (except general interest), plus weighting
  select(PONDERA_ENLINIA, 
         EDAT_3G_30,
         starts_with("INTERES_") & ends_with("_0_10"), 
         -INTERES_POL_0_10) %>% 
  # Put the interest in specific topics in long format
  pivot_longer(-c(PONDERA_ENLINIA, EDAT_3G_30),
               names_to = "POLITICAL_TOPIC",
               values_to = "VALUE_0_10") %>% 
  filter(!is.na(VALUE_0_10)) %>% 
  mutate(POLITICAL_TOPIC = case_match(
    POLITICAL_TOPIC,
    "INTERES_ACCES_HAB_0_10" ~ "Housing\naccess",
    "INTERES_PREC_LAB_0_10" ~ "Job\ninsecurity",
    "INTERES_ELECCIONS_0_10" ~ "Elections",
    "INTERES_PARTITS_0_10" ~ "Political parties",
    "INTERES_CANVI_CLI_0_10" ~ "Climate\nChange",
    "INTERES_FEMINISME_0_10" ~ "Feminism",
    "INTERES_LLENGUA_CAT_0_10" ~ "Language",
    "INTERES_REL_CAT_ESP_0_10" ~ "Catalan-Spanish\nrelations",
    "INTERES_FUNC_ECO_0_10" ~ "Economy",
    "INTERES_SEG_CIUTADANA_0_10" ~ "Security"
  )) %>% 
  mutate(POLITICAL_TOPIC = factor(
    POLITICAL_TOPIC,
    levels = c("Housing\naccess",
               "Job\ninsecurity",
               "Elections",
               "Political parties",
               "Climate\nChange",
               "Feminism",
               "Language",
               "Catalan-Spanish\nrelations",
               "Economy",
               "Security")
  )) 

# 1) Weighted mean by topic and age group
topic_means <- data_long %>%
  group_by(EDAT_3G_30, POLITICAL_TOPIC) %>%
  summarise(
    avg = weighted.mean(VALUE_0_10, w = PONDERA_ENLINIA, na.rm = TRUE),
    .groups = "drop"
  )

# 2) Top 10 within each age group + rank
top10 <- topic_means %>%
  group_by(EDAT_3G_30) %>%
  arrange(desc(avg), .by_group = TRUE) %>%
  slice_head(n = 10) %>%
  mutate(rank = row_number()) %>%
  ungroup()

# 3) Reshape to wide: for each age group -> (topic, avg)
top10_wide <- top10 %>%
  select(rank, EDAT_3G_30, topic = POLITICAL_TOPIC, avg) %>%
  pivot_wider(
    names_from  = EDAT_3G_30,
    values_from = c(topic, avg),
    names_glue  = "{EDAT_3G_30}_{.value}"
  )

# 4) Overall (across ALL topics in data_long) weighted mean and weighted sd by age group
overall <- data_long %>%
  group_by(EDAT_3G_30) %>%
  summarise(
    mean = weighted.mean(VALUE_0_10, w = PONDERA_ENLINIA, na.rm = TRUE),
    sd   = sqrt(weighted.mean((VALUE_0_10 - mean)^2, w = PONDERA_ENLINIA, na.rm = TRUE)),
    .groups = "drop"
  )

# --- helper: pull overall stats by age label ---
get_overall <- function(age, stat) {
  overall %>%
    filter(EDAT_3G_30 == age) %>%
    pull({{ stat }})
}

# --- build final table (adds Mean + St. Dev. rows) ---
final_tbl <- top10_wide %>%
  mutate(rank = as.character(rank)) %>%
  bind_rows(
    tibble(
      rank = "Mean",
      `16-29_topic` = "", `16-29_avg` = get_overall("16-29", mean),
      `30-64_topic` = "", `30-64_avg` = get_overall("30-64", mean),
      `65 or more_topic` = "", `65 or more_avg` = get_overall("65 or more", mean)
    ),
    tibble(
      rank = "St. Dev.",
      `16-29_topic` = "", `16-29_avg` = get_overall("16-29", sd),
      `30-64_topic` = "", `30-64_avg` = get_overall("30-64", sd),
      `65 or more_topic` = "", `65 or more_avg` = get_overall("65 or more", sd)
    )
  )

# --- render with gt (layout like the image: 3 blocks with topic + value) ---
gt_table <- final_tbl %>%
  gt::gt() %>%
  gt::tab_spanner(label = "16-29",      columns = c(`16-29_topic`, `16-29_avg`)) %>%
  gt::tab_spanner(label = "30-64",      columns = c(`30-64_topic`, `30-64_avg`)) %>%
  gt::tab_spanner(label = "65 or more", columns = c(`65 or more_topic`, `65 or more_avg`)) %>%
  gt::cols_label(
    rank = "",
    `16-29_topic` = "", `16-29_avg` = "",
    `30-64_topic` = "", `30-64_avg` = "",
    `65 or more_topic` = "", `65 or more_avg` = ""
  ) %>%
  gt::fmt_number(columns = ends_with("_avg"), decimals = 2) %>%
  gt::cols_align(align = "right", columns = rank) %>%
  gt::cols_align(align = "left",  columns = ends_with("_topic")) %>%
  gt::cols_align(align = "right", columns = ends_with("_avg")) %>%
  gt::tab_options(
    table.font.size = gt::px(12),
    data_row.padding = gt::px(3),
    table.border.top.style = "solid",
    table.border.bottom.style = "solid",
    column_labels.border.bottom.style = "solid"
  )

gt::gtsave(gt_table, filename = "figures/Table 1.html")
```

# Figure 4

Main effects of the experiment on general political interest.

```{r Figure_4}
df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  ggstatsplot::ggbetweenstats(
    x = GRUP_EXPERIMENT, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 0.5,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = FALSE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Treatment",
    ylab = "Interest in politics (0-10)"
    )

ggsave(
  "figures/Figure 4.png",
  width = 18,
  height = 12.5,
  units = "cm"
)
```


# Figure 5

Heterogeneous treatment effects by age.

This code creates six different plots and combines them.

```{r Figure_5}
p_control <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "Control") %>% 
  ggstatsplot::ggbetweenstats(
    x = EDAT_3G_30, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 1,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Age group",
    ylab = "Interest in politics (0-10)",
    title = "Control group"
    )

p_youth <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "Youth issues") %>% 
  ggstatsplot::ggbetweenstats(
    x = EDAT_3G_30, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 1,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Age group",
    ylab = "Interest in politics (0-10)",
    title = "Youth issues"
    )

p_traditional <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "Traditional politics") %>% 
  ggstatsplot::ggbetweenstats(
    x = EDAT_3G_30, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 1,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Age group",
    ylab = "Interest in politics (0-10)",
    title = "Traditional politics"
    )

p_current <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "Current issues") %>% 
  ggstatsplot::ggbetweenstats(
    x = EDAT_3G_30, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 1,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Age group",
    ylab = "Interest in politics (0-10)",
    title = "Current issues"
    )

p_national <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "National identity") %>% 
  ggstatsplot::ggbetweenstats(
    x = EDAT_3G_30, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 1,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Age group",
    ylab = "Interest in politics (0-10)",
    title = "National identity"
    )

p_senior <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "Senior issues") %>% 
  ggstatsplot::ggbetweenstats(
    x = EDAT_3G_30, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 1,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Age group",
    ylab = "Interest in politics (0-10)",
    title = "Senior issues"
    )

ggstatsplot::combine_plots(
  list(p_control, p_youth, p_traditional, p_current, p_national, p_senior),
  plotgrid.args = list(nrow = 2),
  annotation.args = list(
    title = "Comparison of the age-gap in the different treatment groups",
    caption = "p-values only shown if significant"
  )
)

ggsave(
  "figures/Figure 5.png",
  width = 35,
  height = 24,
  units = "cm"
)
```


# Figure 6

Marginal effect of treatments, by age group.

```{r Figure_6}
# 1. Fit linear model -----------------------------------------------------
#   - Outcome: INTERES_POL_0_10 (0–10 political interest scale)
#   - Predictors: experimental group × age group
#   - Reference category for GRUP_EXPERIMENT is set to "Control",
#     so all marginal effects are interpreted as Treatment − Control.

model <- df %>%
  mutate(
    INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10),
    GRUP_EXPERIMENT  = relevel(GRUP_EXPERIMENT, ref = "Control")
  ) %>%
  lm(INTERES_POL_0_10 ~ GRUP_EXPERIMENT * EDAT_3G_30, data = .)

# 2. Estimated marginal means ---------------------------------------------
# EMMs of GRUP_EXPERIMENT within each age group (EDAT_3G_30)

emm <- emmeans(model, ~ GRUP_EXPERIMENT | EDAT_3G_30)

# 3. Marginal effects: Treatment − Control --------------------------------
# 'trt.vs.ctrl' compares each non-reference level to the reference
# (here "Control"). We also keep the confidence intervals even if
# they are not plotted, for reporting in tables if needed.

meff <- emm %>%
  contrast(method = "trt.vs.ctrl") %>%
  confint() %>%                 # adds lower.CL and upper.CL
  as.data.frame()

# 4. Clean up labels and factor ordering ----------------------------------
#   - Strip the " - Control" part from contrast labels
#   - Set a meaningful order for the treatment topics on the x-axis

meff <- meff %>%
  mutate(
    GRUP_EXPERIMENT = gsub(" - .*$", "", contrast),
    EDAT_3G_30      = factor(EDAT_3G_30),   # set levels manually if needed
    GRUP_EXPERIMENT = factor(
      GRUP_EXPERIMENT,
      levels = c("Youth issues",
                 "Traditional politics",
                 "Current issues",
                 "National identity",
                 "Senior issues")
    )
  )

# 5. Lollipop plot of marginal effects ------------------------------------
#   - One lollipop per treatment × age group
#   - Stems are vertical (from 0 to estimate)
#   - Age groups are distinguished by color

pd <- position_dodge(width = 0.6)

ggplot(
  meff,
  aes(
    x     = GRUP_EXPERIMENT,
    y     = estimate,
    color = EDAT_3G_30,
    group = EDAT_3G_30
  )
) +
  # Reference line at zero (no treatment effect)
  geom_hline(yintercept = 0, linetype = "dashed", linewidth = 0.4) +

  # Lollipop stems (Treatment − Control)
  geom_linerange(
    aes(ymin = 0, ymax = estimate),
    position = pd,
    linewidth = 0.9,
    alpha = 0.9
  ) +

  # Lollipop heads
  geom_point(
    position = pd,
    size = 3.2,
    stroke = 0.7
  ) +

  # Color palette for age groups
  scale_color_brewer(palette = "Dark2") +

  # More formal theme (similar to ggstatsplot style)
  theme_minimal(base_size = 13) +
  theme(
    panel.grid.major.x = element_blank(),
    panel.grid.minor   = element_blank(),
    axis.title.x       = element_text(margin = margin(t = 8)),
    axis.title.y       = element_text(margin = margin(r = 8)),
    legend.position    = "bottom",
    legend.title       = element_text(face = "bold"),
    plot.title         = element_text(face = "bold", hjust = 0),
    plot.subtitle      = element_text(hjust = 0)
  ) +

  labs(
    x        = "Treatment",
    y        = "Marginal effect (Treatment − Control)",
    color    = "Age group"
  )

ggsave(
  "figures/Figure 6.png",
  width = 22,
  height = 12,
  units = "cm"
)
```


# Figure 7

Percentage of topics mentioned in the open-ended question based on priming exposure.

```{r Figure_7}
binary_vars_experiment <- c(
  "INT_POL_CODI_HABITATGE_1", 
  "INT_POL_CODI_PRECARIETAT_2", 
  "INT_POL_CODI_ELECCIONS_3",
  "INT_POL_CODI_PARTITS_4", 
  "INT_POL_CODI_CANVI_CLIMATIC_5", 
  "INT_POL_CODI_FEMINISME_6",
  "INT_POL_CODI_LLENGUA_7", 
  "INT_POL_CODI_INDEPENDENCIA_8", 
  "INT_POL_CODI_ECONOMIA_9",
  "INT_POL_CODI_SEGURETAT_10"
  )

variable_names_experiment <- c(
  "Housing\naccess", 
  "Job\ninsecurity", 
  "Elections", 
  "Political parties", 
  "Climate Change",
  "Feminism", 
  "Language", 
  "Catalan-Spanish\nrelations", 
  "Economy", 
  "Security"
)

result3 <- data.frame(Binary_Variable = character(),
                     Group = character(),
                     Count = numeric(),
                     stringsAsFactors = FALSE)

# Loop through binary variables
for (i in seq_along(binary_vars_experiment)) {
  # Loop through groups
  binary_var <- binary_vars_experiment[i]
  variable_name <- variable_names_experiment[i]
  
  for (group in levels(df$GRUP_EXPERIMENT)) {
    # Calculate the count of 1 for the binary variable and group
    count <- sum(df[[binary_var]][df$GRUP_EXPERIMENT == group])
    
    # Create a new row for the result dataframe
    new_row <- data.frame(Binary_Variable = variable_name,
                          Group = group,
                          Count = count,
                          stringsAsFactors = FALSE)
    
    # Append the new row to the result dataframe
    result3 <- rbind(result3, new_row)
  }
}

n_cases_result3 <- result3 %>% 
  mutate(Binary_Variable = factor(Binary_Variable,
                                  levels = c(
                                    "Housing\naccess", 
                                    "Job\ninsecurity", 
                                    "Elections", 
                                    "Political parties", 
                                    "Climate Change",
                                    "Feminism", 
                                    "Language", 
                                    "Catalan-Spanish\nrelations", 
                                    "Economy", 
                                    "Security"
                                  ))) %>% 
  summarise(N = sum(Count), .by = Binary_Variable)

result3 %>% 
  group_by(Group) %>% 
  mutate(perc = round(100*(Count / sum(Count)), 2)) %>% 
  ungroup() %>% 
  mutate(Binary_Variable = factor(Binary_Variable,
                                  levels = c(
                                    "Housing\naccess", 
                                    "Job\ninsecurity", 
                                    "Elections", 
                                    "Political parties", 
                                    "Climate Change",
                                    "Feminism", 
                                    "Language", 
                                    "Catalan-Spanish\nrelations", 
                                    "Economy", 
                                    "Security"
                                  ))) %>% 
  mutate(Group = factor(Group,
                        levels = c(
                          "Control",
                          "Youth issues",
                          "Traditional politics",
                          "Current issues",
                          "National identity",
                          "Senior issues"
                        ))) %>% 
  mutate(Framing = case_when(
    Group == "Youth issues" & 
      Binary_Variable %in% c("Housing\naccess", 
                             "Job\ninsecurity") ~ "Framing",
    Group == "Traditional politics" & 
      Binary_Variable %in% c("Elections", 
                             "Political parties") ~ "Framing",
    Group == "Current issues" & 
      Binary_Variable %in% c("Climate Change", 
                             "Feminism") ~ "Framing",
    Group == "National identity" & 
      Binary_Variable %in% c("Language", 
                             "Catalan-Spanish\nrelations") ~ "Framing",
    Group == "Senior issues" & 
      Binary_Variable %in% c("Economy", 
                             "Security") ~ "Framing",
    TRUE ~ "No framing"
  )) %>% 
  mutate(Framing = factor(Framing, levels = c("Framing", "No framing"))) %>% 
  left_join(n_cases_result3, by = "Binary_Variable") %>% 
  ggplot(aes(x = Binary_Variable, y = perc, color = Framing)) +
  geom_point(size = 2.5) +
  labs(x = "Open-ended question responses (classified)", 
       y = "Percentage") +
  theme_minimal() +
  theme(legend.position = "top") +
  scale_color_manual(values = c("red", "darkgray"),
                     labels = c("Priming", "No priming")) +
  labs(color = "")

ggsave(
  "figures/Figure 7.png",
  width = 27,
  height = 16,
  units = "cm"
)
```

# Figure A1

Age gap in control group with alternative age groups

```{r Figure_A1}
df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "Control") %>% 
  # Create alternative age groups
  mutate(EDAT_4G_30 = factor(case_when(
      EDAT >= 16 & EDAT <= 29 ~ "16-29",
      EDAT >= 30 & EDAT <= 49 ~ "30-49",
      EDAT >= 50 & EDAT <= 64 ~ "50-64",
      EDAT >= 65 & EDAT <= 110 ~ "65 or more",
      TRUE ~ NA_character_), 
    levels = c("16-29", "30-49", "50-64", "65 or more")
  )) %>%
  # Plot
  ggstatsplot::ggbetweenstats(
    x = EDAT_4G_30, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 0.5,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Age group",
    ylab = "Interest in politics (0-10)",
    title = "Control group"
    )

ggsave(
  "figures/Figure A1.png",
  width = 24,
  height = 15,
  units = "cm"
)
```

# Figure A2

Heterogeneous effects by age with alternative age groups

This code creates 6 different plots and combines them.

```{r Figure_A2}
p_control <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "Control") %>% 
  mutate(EDAT_4G_30 = factor(case_when(
    EDAT >= 16 & EDAT <= 29 ~ "16-29",
    EDAT >= 30 & EDAT <= 49 ~ "30-49",
    EDAT >= 50 & EDAT <= 64 ~ "50-64",
    EDAT >= 65 & EDAT <= 110 ~ "65 or more",
    TRUE ~ NA_character_
  ), levels = c("16-29", "30-49", "50-64", "65 or more")
  )) %>%
  ggstatsplot::ggbetweenstats(
    x = EDAT_4G_30, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 1,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Age group",
    ylab = "Interest in politics (0-10)",
    title = "Control group"
    )

p_youth <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "Youth issues") %>% 
  mutate(EDAT_4G_30 = factor(case_when(
    EDAT >= 16 & EDAT <= 29 ~ "16-29",
    EDAT >= 30 & EDAT <= 49 ~ "30-49",
    EDAT >= 50 & EDAT <= 64 ~ "50-64",
    EDAT >= 65 & EDAT <= 110 ~ "65 or more",
    TRUE ~ NA_character_
  ), levels = c("16-29", "30-49", "50-64", "65 or more")
  )) %>%
  ggstatsplot::ggbetweenstats(
    x = EDAT_4G_30,  
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 1,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Age group",
    ylab = "Interest in politics (0-10)",
    title = "Youth issues"
    )

p_traditional <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "Traditional politics") %>% 
  mutate(EDAT_4G_30 = factor(case_when(
    EDAT >= 16 & EDAT <= 29 ~ "16-29",
    EDAT >= 30 & EDAT <= 49 ~ "30-49",
    EDAT >= 50 & EDAT <= 64 ~ "50-64",
    EDAT >= 65 & EDAT <= 110 ~ "65 or more",
    TRUE ~ NA_character_
  ), levels = c("16-29", "30-49", "50-64", "65 or more")
  )) %>%
  ggstatsplot::ggbetweenstats(
    x = EDAT_4G_30,  
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 1,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Age group",
    ylab = "Interest in politics (0-10)",
    title = "Traditional politics"
    )

p_current <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "Current issues") %>% 
  mutate(EDAT_4G_30 = factor(case_when(
    EDAT >= 16 & EDAT <= 29 ~ "16-29",
    EDAT >= 30 & EDAT <= 49 ~ "30-49",
    EDAT >= 50 & EDAT <= 64 ~ "50-64",
    EDAT >= 65 & EDAT <= 110 ~ "65 or more",
    TRUE ~ NA_character_
  ), levels = c("16-29", "30-49", "50-64", "65 or more")
  )) %>%
  ggstatsplot::ggbetweenstats(
    x = EDAT_4G_30, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 1,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Age group",
    ylab = "Interest in politics (0-10)",
    title = "Current issues"
    )

p_national <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "National identity") %>% 
  mutate(EDAT_4G_30 = factor(case_when(
    EDAT >= 16 & EDAT <= 29 ~ "16-29",
    EDAT >= 30 & EDAT <= 49 ~ "30-49",
    EDAT >= 50 & EDAT <= 64 ~ "50-64",
    EDAT >= 65 & EDAT <= 110 ~ "65 or more",
    TRUE ~ NA_character_
  ), levels = c("16-29", "30-49", "50-64", "65 or more")
  )) %>%
  ggstatsplot::ggbetweenstats(
    x = EDAT_4G_30,  
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 1,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Age group",
    ylab = "Interest in politics (0-10)",
    title = "National identity"
    )

p_senior <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "Senior issues") %>% 
  mutate(EDAT_4G_30 = factor(case_when(
    EDAT >= 16 & EDAT <= 29 ~ "16-29",
    EDAT >= 30 & EDAT <= 49 ~ "30-49",
    EDAT >= 50 & EDAT <= 64 ~ "50-64",
    EDAT >= 65 & EDAT <= 110 ~ "65 or more",
    TRUE ~ NA_character_
  ), levels = c("16-29", "30-49", "50-64", "65 or more")
  )) %>%
  ggstatsplot::ggbetweenstats(
    x = EDAT_4G_30, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 1,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Age group",
    ylab = "Interest in politics (0-10)",
    title = "Senior issues"
    )

ggstatsplot::combine_plots(
  list(p_control, p_youth, p_traditional, p_current, p_national, p_senior),
  plotgrid.args = list(nrow = 2),
  annotation.args = list(
    title = "Comparison of the age-gap in the different treatment groups",
    caption = "p-values only shown if significant"
  )
)

ggsave(
  "figures/Figure A2.png",
  width = 35,
  height = 24,
  units = "cm"
)
```

# Figure A3

Experimental results after regression analysis not adjusted by baseline variables 

```{r Figure_A3}
model <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  lm(INTERES_POL_0_10 ~ GRUP_EXPERIMENT * EDAT_3G_30, data = .)

# Get unique non-NA values for both factors
valid_groups <- df %>% filter(!is.na(GRUP_EXPERIMENT)) %>% pull(GRUP_EXPERIMENT) %>% unique()
valid_ages <- df %>% filter(!is.na(EDAT_3G_30)) %>% pull(EDAT_3G_30) %>% unique()

# Create grid of valid combinations only
newdata <- expand.grid(
  GRUP_EXPERIMENT = valid_groups,
  EDAT_3G_30 = valid_ages
)

# Add predicted values and 95% CI using base R predict()
predictions <- predict(model, newdata = newdata, interval = "confidence", level = 0.95)

# Combine predictions into the newdata frame
prediction_data <- cbind(newdata, predictions)

# Plot
ggplot(prediction_data, aes(x = EDAT_3G_30, y = fit, color = EDAT_3G_30, group = EDAT_3G_30)) +
  geom_point(position = position_dodge(width = 0.4), size = 3) +
  geom_errorbar(aes(ymin = lwr, ymax = upr),
                position = position_dodge(width = 0.4),
                width = 0.2,
                linewidth = 1) +
  geom_line(position = position_dodge(width = 0.4), linewidth = 1) +
  scale_color_brewer(palette = "Dark2") +
  theme_minimal() +
  theme(legend.position = "none") +
  labs(
    title = "Predicted Interest in Politics by Age and Experimental Group",
    subtitle = "Model: Interest ~ Treatment * Age",
    x = "Age group",
    y = "Predicted interest (0–10)"
  ) +
  facet_wrap(~ GRUP_EXPERIMENT)

ggsave(
  "figures/Figure A3.png",
  width = 24,
  height = 15,
  units = "cm"
)
```

# Figure A4

Experimental results after regression analysis adjusted by baseline variables 

```{r Figure_A4}
# Step 1: Fit the model
model <- df %>%
  mutate(
    INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10),
    ESTUDIS = case_when(
      ESTUDIS_1_6 == 1 ~ 1,
      ESTUDIS_1_6 <= 4 ~ 2,
      ESTUDIS_1_6 <= 6 ~ 3,
      ESTUDIS_1_6 == 7777 ~ NA_real_,
      TRUE ~ NA_real_
    ),
    ESTUDIS = factor(ESTUDIS, levels = c(1, 2, 3)),
    LLOC_NAIX = factor(ifelse(LLOC_NAIX %in% c(1, 2, 5), LLOC_NAIX, NA))
  ) %>%
  lm(INTERES_POL_0_10 ~ GRUP_EXPERIMENT * EDAT_3G_30 + SEXE + ESTUDIS + LLOC_NAIX, data = .)

# Step 2: Manually set control variables to reference or common values
newdata <- expand.grid(
  GRUP_EXPERIMENT = unique(na.omit(df$GRUP_EXPERIMENT)),
  EDAT_3G_30 = unique(na.omit(df$EDAT_3G_30)),
  SEXE = "Men",                  # assuming 1 = most common (e.g., male)
  ESTUDIS = factor(2, levels = c(1, 2, 3)),  # medium education
  LLOC_NAIX = factor(1)      # reference category, e.g., born in Catalonia
)

# Step 3: Predict with confidence intervals
predictions <- predict(model, newdata = newdata, interval = "confidence")
prediction_data <- cbind(newdata, predictions)

# Step 4: Plot with Dark2 palette
ggplot(prediction_data, aes(x = EDAT_3G_30, y = fit, color = EDAT_3G_30, group = EDAT_3G_30)) +
  geom_point(position = position_dodge(width = 0.4), size = 3) +
  geom_errorbar(aes(ymin = lwr, ymax = upr),
                position = position_dodge(width = 0.4),
                width = 0.2,
                linewidth = 1) +
  geom_line(position = position_dodge(width = 0.4), linewidth = 1) +
  scale_color_brewer(palette = "Dark2") +
  theme_minimal() +
  theme(legend.position = "none") +
  labs(
    title = "Predicted Interest in Politics by Age and Experimental Group",
    subtitle = "Model: Interest ~ Treatment * Age + Controls by sex, education and birthplace",
    x = "Age group",
    y = "Predicted interest (0–10)",
    color = "Age group"
  ) +
  facet_wrap(~ GRUP_EXPERIMENT)

ggsave(
  "figures/Figure A4.png",
  width = 24,
  height = 15,
  units = "cm"
)
```

# Figure A5

Mean Interest in Political Topics by Age Group (Centered by Group Mean, with 95% Confidence Intervals) 

```{r Figure_A5}
# Reshape and label data
data_long_age <- df %>%
  select(PONDERA_ENLINIA,
         EDAT_3G_30,
         starts_with("INTERES_") & ends_with("_0_10"),
         -INTERES_POL_0_10) %>%
  pivot_longer(
    cols = -c(PONDERA_ENLINIA, EDAT_3G_30),
    names_to = "POLITICAL_TOPIC",
    values_to = "VALUE_0_10"
  ) %>%
  filter(!is.na(VALUE_0_10)) %>%
  mutate(
    POLITICAL_TOPIC = case_match(
      POLITICAL_TOPIC,
      "INTERES_ACCES_HAB_0_10" ~ "Housing\naccess",
      "INTERES_PREC_LAB_0_10" ~ "Job\ninsecurity",
      "INTERES_ELECCIONS_0_10" ~ "Elections",
      "INTERES_PARTITS_0_10" ~ "Political parties",
      "INTERES_CANVI_CLI_0_10" ~ "Climate\nChange",
      "INTERES_FEMINISME_0_10" ~ "Feminism",
      "INTERES_LLENGUA_CAT_0_10" ~ "Language",
      "INTERES_REL_CAT_ESP_0_10" ~ "Catalan-Spanish\nrelations",
      "INTERES_FUNC_ECO_0_10" ~ "Economy",
      "INTERES_SEG_CIUTADANA_0_10" ~ "Security"
    ),
    POLITICAL_TOPIC = factor(
      POLITICAL_TOPIC,
      levels = c("Housing\naccess",
                 "Job\ninsecurity",
                 "Elections",
                 "Political parties",
                 "Climate\nChange",
                 "Feminism",
                 "Language",
                 "Catalan-Spanish\nrelations",
                 "Economy",
                 "Security")
    )
  )

# STEP 1: Compute group-wise average interest across all topics
group_means <- data_long_age %>%
  group_by(EDAT_3G_30) %>%
  summarise(
    overall_mean = weighted.mean(VALUE_0_10, PONDERA_ENLINIA),
    .groups = "drop"
  )

# STEP 2: Merge and calculate topic-specific means and CIs
aux_age_centered <- data_long_age %>%
  left_join(group_means, by = "EDAT_3G_30") %>%
  group_by(EDAT_3G_30, POLITICAL_TOPIC) %>%
  summarise(
    mean_topic = weighted.mean(VALUE_0_10, PONDERA_ENLINIA),
    n = n(),
    CI = 1.96 * (sqrt(Hmisc::wtd.var(VALUE_0_10, w = PONDERA_ENLINIA)) / sqrt(n)),
    overall_mean = first(overall_mean),
    .groups = "drop"
  ) %>%
  mutate(
    centered_mean = mean_topic - overall_mean,
    CI_inf = centered_mean - CI,
    CI_sup = centered_mean + CI
  )

# STEP 3: Plot centered means with error bars
ggplot(aux_age_centered, aes(x = POLITICAL_TOPIC, y = centered_mean, color = EDAT_3G_30)) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "grey60") +
  geom_point(position = position_dodge(width = 0.6), size = 2) +
  geom_errorbar(aes(ymin = CI_inf, ymax = CI_sup),
                position = position_dodge(width = 0.6),
                width = 0.2,
                linewidth = 1) +
  scale_color_brewer(palette = "Dark2") +
  theme_minimal() +
  labs(
    x = "Political topic",
    y = "Interest relative to group mean",
    color = "Age group"
  ) +
  theme(legend.position = "bottom")

ggsave(
  "figures/Figure A5.png",
  width = 24,
  height = 15,
  units = "cm"
)
```

# Figure B1

Main effects of the experiment by age group

This code creates 6 different plots and combines them.

```{r Figure_B1}
p_1 <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(EDAT_3G_30 == "16-29") %>% 
  ggstatsplot::ggbetweenstats(
    x = GRUP_EXPERIMENT, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 0.5,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    var.equal = TRUE,
    pairwise.display = "significant",
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Treatment",
    ylab = "Interest in politics (0-10)",
    title = "Age: 16-29"
    )

p_2 <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(EDAT_3G_30 == "30-64") %>% 
  ggstatsplot::ggbetweenstats(
    x = GRUP_EXPERIMENT, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 0.5,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Treatment",
    ylab = "Interest in politics (0-10)",
    title = "Age: 30-64"
    )

p_3 <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(EDAT_3G_30 == "65 or more") %>% 
  ggstatsplot::ggbetweenstats(
    x = GRUP_EXPERIMENT, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 0.5,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Treatment",
    ylab = "Interest in politics (0-10)",
    title = "Age: 65 or more"
    )

ggstatsplot::combine_plots(
  list(p_1, p_2, p_3),
  plotgrid.args = list(nrow = 1),
  annotation.args = list(
    title = "Comparison of the main effects in different age groups",
    caption = "p-values only shown if significant"
  )
)


ggsave(
  "figures/Figure B1.png",
  width = 45,
  height = 18,
  units = "cm"
)
```

# Figure B2

Generation-gap in control group

```{r Figure_B2}
df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "Control") %>% 
  filter(EDAT_GENERACIONS != 7777) %>% 
  mutate(EDAT_GENERACIONS = as.factor(EDAT_GENERACIONS)) %>% 
  mutate(EDAT_GENERACIONS = fct_recode(
    EDAT_GENERACIONS,
    "The Silent Generation" = "2",
    "Baby Boomers" = "3",
    "Generation X" = "4",
    "Millennials" = "5",
    "Generation Z" = "6"
  )) %>% 
  ggstatsplot::ggbetweenstats(
    x = EDAT_GENERACIONS, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 0.5,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Generation",
    ylab = "Interest in politics (0-10)",
    title = "Control group"
    )

ggsave(
  "figures/Figure B2.png",
  width = 27,
  height = 16,
  units = "cm"
)
```

# Figure B3

Heterogeneous effects by generation

```{r Figure_B3}
p_control <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "Control") %>% 
  filter(EDAT_GENERACIONS != 7777) %>% 
  mutate(EDAT_GENERACIONS = as.factor(EDAT_GENERACIONS)) %>% 
  mutate(EDAT_GENERACIONS = fct_recode(
    EDAT_GENERACIONS,
    "The Silent Generation" = "2",
    "Baby Boomers" = "3",
    "Generation X" = "4",
    "Millennials" = "5",
    "Generation Z" = "6"
  )) %>% 
  ggstatsplot::ggbetweenstats(
    x = EDAT_GENERACIONS, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 0.6,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Generation",
    ylab = "Interest in politics (0-10)",
    title = "Control group"
    )


p_youth <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "Youth issues") %>% 
  filter(EDAT_GENERACIONS != 7777) %>% 
  mutate(EDAT_GENERACIONS = as.factor(EDAT_GENERACIONS)) %>% 
  mutate(EDAT_GENERACIONS = fct_recode(
    EDAT_GENERACIONS,
    "The Silent Generation" = "2",
    "Baby Boomers" = "3",
    "Generation X" = "4",
    "Millennials" = "5",
    "Generation Z" = "6"
  )) %>% 
  ggstatsplot::ggbetweenstats(
    x = EDAT_GENERACIONS,  
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 0.6,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Generation",
    ylab = "Interest in politics (0-10)",
    title = "Youth issues"
    )


p_traditional <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "Traditional politics") %>% 
  filter(EDAT_GENERACIONS != 7777) %>% 
  mutate(EDAT_GENERACIONS = as.factor(EDAT_GENERACIONS)) %>% 
  mutate(EDAT_GENERACIONS = fct_recode(
    EDAT_GENERACIONS,
    "The Silent Generation" = "2",
    "Baby Boomers" = "3",
    "Generation X" = "4",
    "Millennials" = "5",
    "Generation Z" = "6"
  )) %>% 
  ggstatsplot::ggbetweenstats(
    x = EDAT_GENERACIONS, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 0.6,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Generation",
    ylab = "Interest in politics (0-10)",
    title = "Traditional politics"
    )

p_current <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "Current issues") %>% 
  filter(EDAT_GENERACIONS != 7777) %>% 
  mutate(EDAT_GENERACIONS = as.factor(EDAT_GENERACIONS)) %>% 
  mutate(EDAT_GENERACIONS = fct_recode(
    EDAT_GENERACIONS,
    "The Silent Generation" = "2",
    "Baby Boomers" = "3",
    "Generation X" = "4",
    "Millennials" = "5",
    "Generation Z" = "6"
  )) %>% 
  ggstatsplot::ggbetweenstats(
    x = EDAT_GENERACIONS, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 0.6,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Generation",
    ylab = "Interest in politics (0-10)",
    title = "Current issues"
    )


p_national <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "National identity") %>% 
  filter(EDAT_GENERACIONS != 7777) %>% 
  mutate(EDAT_GENERACIONS = as.factor(EDAT_GENERACIONS)) %>% 
  mutate(EDAT_GENERACIONS = fct_recode(
    EDAT_GENERACIONS,
    "The Silent Generation" = "2",
    "Baby Boomers" = "3",
    "Generation X" = "4",
    "Millennials" = "5",
    "Generation Z" = "6"
  )) %>% 
  ggstatsplot::ggbetweenstats(
    x = EDAT_GENERACIONS,   
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 0.6,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Generation",
    ylab = "Interest in politics (0-10)",
    title = "National identity"
    )


p_senior <- df %>% 
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>% 
  filter(GRUP_EXPERIMENT == "Senior issues") %>% 
  filter(EDAT_GENERACIONS != 7777) %>% 
  mutate(EDAT_GENERACIONS = as.factor(EDAT_GENERACIONS)) %>% 
  mutate(EDAT_GENERACIONS = fct_recode(
    EDAT_GENERACIONS,
    "The Silent Generation" = "2",
    "Baby Boomers" = "3",
    "Generation X" = "4",
    "Millennials" = "5",
    "Generation Z" = "6"
  )) %>% 
  ggstatsplot::ggbetweenstats(
    x = EDAT_GENERACIONS, 
    y = INTERES_POL_0_10,
    point.args = list(
      position = ggplot2::position_jitterdodge(
        jitter.width = 0.6,
        jitter.height = 0.6
        ), 
      alpha = 0.15, 
      size = 1.25, 
      na.rm = TRUE),
    type = "parametric",
    pairwise.display = "significant",
    var.equal = TRUE,
    p.adjust.method = "none",
    results.subtitle = FALSE,     
    centrality.plotting = TRUE,    
    centrality.type = "parametric",
    pairwise.annotation = "p.value",
    xlab = "Generation",
    ylab = "Interest in politics (0-10)",
    title = "Senior issues"
    )

ggstatsplot::combine_plots(
  list(p_control, p_youth, p_traditional, p_current, p_national, p_senior),
  plotgrid.args = list(nrow = 2),
  annotation.args = list(
    title = "Comparison of the generational gap in the different treatment groups",
    caption = "p-values only shown if significant"
  )
)

ggsave(
  "figures/Figure B3.png",
  width = 45,
  height = 30,
  units = "cm"
)
```

# Table C1

Levene’s Test Results for Homogeneity of Variance by Figure and Comparison Group

```{r Table_C1}
# Helper to extract p-value from car::leveneTest()
get_p <- function(x) {
  as.numeric(x[["Pr(>F)"]][1])
}

# -------------------------
# Compute p-values (MANUAL)
# -------------------------

# Row 1 - Fig. 2 (Control; EDAT_3G_30)
p1 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "Control") %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_3G_30, data = .) %>%
  get_p()

# Row 2 - Fig. 4 (Treatments)
p2 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  car::leveneTest(INTERES_POL_0_10 ~ GRUP_EXPERIMENT, data = .) %>%
  get_p()

# Rows 3 to 8 - Fig. 5 (EDAT_3G_30 within each treatment)
p3 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "Control") %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_3G_30, data = .) %>%
  get_p()

p4 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "Youth issues") %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_3G_30, data = .) %>%
  get_p()

p5 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "Traditional politics") %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_3G_30, data = .) %>%
  get_p()

p6 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "Current issues") %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_3G_30, data = .) %>%
  get_p()

p7 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "National identity") %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_3G_30, data = .) %>%
  get_p()

p8 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "Senior issues") %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_3G_30, data = .) %>%
  get_p()

# Row 9 - Fig. A1 (Control; EDAT_4G_30)
p9 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "Control") %>%
  mutate(
    EDAT_4G_30 = factor(
      case_when(
        EDAT >= 16 & EDAT <= 29 ~ "16-29",
        EDAT >= 30 & EDAT <= 49 ~ "30-49",
        EDAT >= 50 & EDAT <= 64 ~ "50-64",
        EDAT >= 65 & EDAT <= 110 ~ "65+",
        TRUE ~ NA_character_
      ),
      levels = c("16-29", "30-49", "50-64", "65+")
    )
  ) %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_4G_30, data = .) %>%
  get_p()

# Rows 10-15 - Fig. A2 (EDAT_4G_30 within each treatment)
p10 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "Control") %>%
  mutate(
    EDAT_4G_30 = factor(
      case_when(
        EDAT >= 16 & EDAT <= 29 ~ "16-29",
        EDAT >= 30 & EDAT <= 49 ~ "30-49",
        EDAT >= 50 & EDAT <= 64 ~ "50-64",
        EDAT >= 65 & EDAT <= 110 ~ "65+",
        TRUE ~ NA_character_
      ),
      levels = c("16-29", "30-49", "50-64", "65+")
    )
  ) %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_4G_30, data = .) %>%
  get_p()

p11 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "Youth issues") %>%
  mutate(
    EDAT_4G_30 = factor(
      case_when(
        EDAT >= 16 & EDAT <= 29 ~ "16-29",
        EDAT >= 30 & EDAT <= 49 ~ "30-49",
        EDAT >= 50 & EDAT <= 64 ~ "50-64",
        EDAT >= 65 & EDAT <= 110 ~ "65+",
        TRUE ~ NA_character_
      ),
      levels = c("16-29", "30-49", "50-64", "65+")
    )
  ) %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_4G_30, data = .) %>%
  get_p()

p12 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "Traditional politics") %>%
  mutate(
    EDAT_4G_30 = factor(
      case_when(
        EDAT >= 16 & EDAT <= 29 ~ "16-29",
        EDAT >= 30 & EDAT <= 49 ~ "30-49",
        EDAT >= 50 & EDAT <= 64 ~ "50-64",
        EDAT >= 65 & EDAT <= 110 ~ "65+",
        TRUE ~ NA_character_
      ),
      levels = c("16-29", "30-49", "50-64", "65+")
    )
  ) %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_4G_30, data = .) %>%
  get_p()

p13 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "Current issues") %>%
  mutate(
    EDAT_4G_30 = factor(
      case_when(
        EDAT >= 16 & EDAT <= 29 ~ "16-29",
        EDAT >= 30 & EDAT <= 49 ~ "30-49",
        EDAT >= 50 & EDAT <= 64 ~ "50-64",
        EDAT >= 65 & EDAT <= 110 ~ "65+",
        TRUE ~ NA_character_
      ),
      levels = c("16-29", "30-49", "50-64", "65+")
    )
  ) %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_4G_30, data = .) %>%
  get_p()

p14 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "National identity") %>%
  mutate(
    EDAT_4G_30 = factor(
      case_when(
        EDAT >= 16 & EDAT <= 29 ~ "16-29",
        EDAT >= 30 & EDAT <= 49 ~ "30-49",
        EDAT >= 50 & EDAT <= 64 ~ "50-64",
        EDAT >= 65 & EDAT <= 110 ~ "65+",
        TRUE ~ NA_character_
      ),
      levels = c("16-29", "30-49", "50-64", "65+")
    )
  ) %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_4G_30, data = .) %>%
  get_p()

p15 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "Senior issues") %>%
  mutate(
    EDAT_4G_30 = factor(
      case_when(
        EDAT >= 16 & EDAT <= 29 ~ "16-29",
        EDAT >= 30 & EDAT <= 49 ~ "30-49",
        EDAT >= 50 & EDAT <= 64 ~ "50-64",
        EDAT >= 65 & EDAT <= 110 ~ "65+",
        TRUE ~ NA_character_
      ),
      levels = c("16-29", "30-49", "50-64", "65+")
    )
  ) %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_4G_30, data = .) %>%
  get_p()

# Rows 16-18 - Fig. B1 (Treatments within each EDAT_3G_30)
p16 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(EDAT_3G_30 == "16-29") %>%
  car::leveneTest(INTERES_POL_0_10 ~ GRUP_EXPERIMENT, data = .) %>%
  get_p()

p17 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(EDAT_3G_30 == "30-64") %>%
  car::leveneTest(INTERES_POL_0_10 ~ GRUP_EXPERIMENT, data = .) %>%
  get_p()

p18 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(EDAT_3G_30 == "65 or more") %>%
  car::leveneTest(INTERES_POL_0_10 ~ GRUP_EXPERIMENT, data = .) %>%
  get_p()

# Row 19 - Fig. B2 (Control; Generations)
p19 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "Control") %>%
  filter(EDAT_GENERACIONS != 7777) %>%
  mutate(EDAT_GENERACIONS = as.factor(EDAT_GENERACIONS)) %>%
  mutate(EDAT_GENERACIONS = forcats::fct_recode(
    EDAT_GENERACIONS,
    "The Silent Generation" = "2",
    "Baby Boomers" = "3",
    "Generation X" = "4",
    "Millennials" = "5",
    "Generation Z" = "6"
  )) %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_GENERACIONS, data = .) %>%
  get_p()

# Rows 20-25 - Fig. B3 (Generations within each treatment)
p20 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "Control") %>%
  filter(EDAT_GENERACIONS != 7777) %>%
  mutate(EDAT_GENERACIONS = as.factor(EDAT_GENERACIONS)) %>%
  mutate(EDAT_GENERACIONS = forcats::fct_recode(
    EDAT_GENERACIONS,
    "The Silent Generation" = "2",
    "Baby Boomers" = "3",
    "Generation X" = "4",
    "Millennials" = "5",
    "Generation Z" = "6"
  )) %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_GENERACIONS, data = .) %>%
  get_p()

p21 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "Youth issues") %>%
  filter(EDAT_GENERACIONS != 7777) %>%
  mutate(EDAT_GENERACIONS = as.factor(EDAT_GENERACIONS)) %>%
  mutate(EDAT_GENERACIONS = forcats::fct_recode(
    EDAT_GENERACIONS,
    "The Silent Generation" = "2",
    "Baby Boomers" = "3",
    "Generation X" = "4",
    "Millennials" = "5",
    "Generation Z" = "6"
  )) %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_GENERACIONS, data = .) %>%
  get_p()

p22 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "Traditional politics") %>%
  filter(EDAT_GENERACIONS != 7777) %>%
  mutate(EDAT_GENERACIONS = as.factor(EDAT_GENERACIONS)) %>%
  mutate(EDAT_GENERACIONS = forcats::fct_recode(
    EDAT_GENERACIONS,
    "The Silent Generation" = "2",
    "Baby Boomers" = "3",
    "Generation X" = "4",
    "Millennials" = "5",
    "Generation Z" = "6"
  )) %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_GENERACIONS, data = .) %>%
  get_p()

p23 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "Current issues") %>%
  filter(EDAT_GENERACIONS != 7777) %>%
  mutate(EDAT_GENERACIONS = as.factor(EDAT_GENERACIONS)) %>%
  mutate(EDAT_GENERACIONS = forcats::fct_recode(
    EDAT_GENERACIONS,
    "The Silent Generation" = "2",
    "Baby Boomers" = "3",
    "Generation X" = "4",
    "Millennials" = "5",
    "Generation Z" = "6"
  )) %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_GENERACIONS, data = .) %>%
  get_p()

p24 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "National identity") %>%
  filter(EDAT_GENERACIONS != 7777) %>%
  mutate(EDAT_GENERACIONS = as.factor(EDAT_GENERACIONS)) %>%
  mutate(EDAT_GENERACIONS = forcats::fct_recode(
    EDAT_GENERACIONS,
    "The Silent Generation" = "2",
    "Baby Boomers" = "3",
    "Generation X" = "4",
    "Millennials" = "5",
    "Generation Z" = "6"
  )) %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_GENERACIONS, data = .) %>%
  get_p()

p25 <- df %>%
  mutate(INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10)) %>%
  filter(GRUP_EXPERIMENT == "Senior issues") %>%
  filter(EDAT_GENERACIONS != 7777) %>%
  mutate(EDAT_GENERACIONS = as.factor(EDAT_GENERACIONS)) %>%
  mutate(EDAT_GENERACIONS = forcats::fct_recode(
    EDAT_GENERACIONS,
    "The Silent Generation" = "2",
    "Baby Boomers" = "3",
    "Generation X" = "4",
    "Millennials" = "5",
    "Generation Z" = "6"
  )) %>%
  car::leveneTest(INTERES_POL_0_10 ~ EDAT_GENERACIONS, data = .) %>%
  get_p()

# -------------------------
# Build the table (MANUAL)
# -------------------------

levene_tbl <- tibble::tibble(
  Figure = c(
    "Fig. 2",
    "Fig. 4",
    "Fig. 5","Fig. 5","Fig. 5","Fig. 5","Fig. 5","Fig. 5",
    "Fig. A1",
    "Fig. A2","Fig. A2","Fig. A2","Fig. A2","Fig. A2","Fig. A2",
    "Fig. B1","Fig. B1","Fig. B1",
    "Fig. B2",
    "Fig. B3","Fig. B3","Fig. B3","Fig. B3","Fig. B3","Fig. B3"
  ),
  Filter = c(
    "Control group",
    "Control group",
    "Control group",
    "Youth issues",
    "Traditional politics",
    "Current issues",
    "National identity",
    "Senior issues",
    "Control group",
    "Control group",
    "Youth issues",
    "Traditional politics",
    "Current issues",
    "National identity",
    "Senior issues",
    "Age: 16-29",
    "Age: 30-64",
    "Age: +65",
    "Control group",
    "Control group",
    "Youth issues",
    "Traditional politics",
    "Current issues",
    "National identity",
    "Senior issues"
  ),
  `Comparison groups` = c(
    "Age: 16-29 / 30-64 / 65+",
    "Treatments",
    "Age: 16-29 / 30-64 / 65+",
    "Age: 16-29 / 30-64 / 65+",
    "Age: 16-29 / 30-64 / 65+",
    "Age: 16-29 / 30-64 / 65+",
    "Age: 16-29 / 30-64 / 65+",
    "Age: 16-29 / 30-64 / 65+",
    "Age: 16-29 / 30-49 / 50-64 / 65+",
    "Age: 16-29 / 30-49 / 50-64 / 65+",
    "Age: 16-29 / 30-49 / 50-64 / 65+",
    "Age: 16-29 / 30-49 / 50-64 / 65+",
    "Age: 16-29 / 30-49 / 50-64 / 65+",
    "Age: 16-29 / 30-49 / 50-64 / 65+",
    "Age: 16-29 / 30-49 / 50-64 / 65+",
    "Treatments",
    "Treatments",
    "Treatments",
    "Generations",
    "Generations",
    "Generations",
    "Generations",
    "Generations",
    "Generations",
    "Generations"
  ),
  `p-value` = c(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20,p21,p22,p23,p24,p25),
  `Test performed` = c(
    "Student’s t",
    "Games-Howell",
    rep("Student’s t", 23)
  )
)

# -------------------------
# gt table + save to HTML
# -------------------------

gt_table <- levene_tbl %>%
  gt::gt() %>%
  gt::fmt_number(columns = `p-value`, decimals = 4) %>%
  gt::cols_align(align = "left", columns = c(Figure, Filter, `Comparison groups`, `Test performed`)) %>%
  gt::cols_align(align = "right", columns = `p-value`)

gt::gtsave(gt_table, filename = "figures/Table C1.html")

```


# Table D1

Cronbach’s Alpha for Issue Pairs Used in Treatment Groups

```{r Table_D1}
# Helper: get raw alpha from a 2-item selection
get_alpha <- function(data, v1, v2) {
  data %>%
    dplyr::select(dplyr::all_of(c(v1, v2))) %>%
    dplyr::filter(!is.na(.data[[v1]])) %>%
    dplyr::filter(.data[[v1]] <= 10) %>%
    dplyr::filter(.data[[v2]] <= 10) %>%
    psych::alpha() %>%
    { .$total$raw_alpha }
}

# Compute alphas manually
a1 <- get_alpha(df, "INTERES_ACCES_HAB_0_10", "INTERES_PREC_LAB_0_10")
a2 <- get_alpha(df, "INTERES_ELECCIONS_0_10", "INTERES_PARTITS_0_10")
a3 <- get_alpha(df, "INTERES_CANVI_CLI_0_10", "INTERES_FEMINISME_0_10")
a4 <- get_alpha(df, "INTERES_LLENGUA_CAT_0_10", "INTERES_REL_CAT_ESP_0_10")
a5 <- get_alpha(df, "INTERES_FUNC_ECO_0_10", "INTERES_SEG_CIUTADANA_0_10")

# Build the results table (like your example)
alpha_tbl <- tibble::tibble(
  `Topic 1` = c("Housing access", "Elections", "Climate change", "Language", "Economy"),
  `Topic 2` = c("Job insecurity", "Political parties", "Feminism", "Catalan-Spanish\nrelations", "Security"),
  `Cronbach's alpha` = c(a1, a2, a3, a4, a5)
)

# Create table in gt format
gt_table <- alpha_tbl %>%
  gt::gt() %>%
  gt::fmt_number(columns = `Cronbach's alpha`, decimals = 2) %>%
  gt::cols_align(align = "left",  columns = c(`Topic 1`, `Topic 2`)) %>%
  gt::cols_align(align = "right", columns = `Cronbach's alpha`) %>%
  gt::tab_options(
    table.font.size = gt::px(12),
    data_row.padding = gt::px(3),
    table.border.top.style = "solid",
    table.border.bottom.style = "solid",
    table.border.left.style = "solid",
    table.border.right.style = "solid",
    column_labels.border.top.style = "solid",
    column_labels.border.bottom.style = "solid",
    table_body.border.bottom.style = "solid"
  )

# Save as HTML
gt::gtsave(gt_table, filename = "figures/Table D1.html")
```


# Figure E1

Predicted Political Interest by Age Group, Treatment, and Labour Status

```{r Figure_E1}
# Clean and factor labour status
df <- df %>%
  mutate(
    INTERES_POL_0_10 = as.numeric(INTERES_POL_0_10),
    SIT_LAB_1_2 = case_when(
      SIT_LAB == 1 ~ "Working",
      SIT_LAB == 2 ~ "Not working",
      TRUE ~ NA_character_
    )) %>% 
  mutate(
    SIT_LAB_1_2 = factor(SIT_LAB_1_2, levels = c("Working", "Not working"))
  )

# Create prediction grid per subgroup
newdata <- df %>%
  filter(!is.na(GRUP_EXPERIMENT), !is.na(EDAT_3G_30), !is.na(SIT_LAB_1_2)) %>%
  distinct(GRUP_EXPERIMENT, EDAT_3G_30, SIT_LAB_1_2)

# Fit models and predict separately per subgroup
prediction_data <- newdata %>%
  group_by(SIT_LAB_1_2) %>%
  group_modify(~ {
    m <- lm(INTERES_POL_0_10 ~ GRUP_EXPERIMENT * EDAT_3G_30,
            data = df %>% filter(SIT_LAB_1_2 == .y$SIT_LAB_1_2))
    preds <- predict(m, newdata = .x, interval = "confidence")
    bind_cols(.x, as.data.frame(preds))
  }) %>%
  ungroup()

ggplot(prediction_data, aes(x = EDAT_3G_30, y = fit, color = EDAT_3G_30, group = EDAT_3G_30)) +
  geom_point(position = position_dodge(width = 0.4), size = 3) +
  geom_errorbar(aes(ymin = lwr, ymax = upr),
                width = 0.2,
                position = position_dodge(width = 0.4),
                linewidth = 1) +
  geom_line(position = position_dodge(width = 0.4), linewidth = 1) +
  scale_color_brewer(palette = "Dark2") +
  facet_grid(rows = vars(SIT_LAB_1_2), cols = vars(GRUP_EXPERIMENT)) +
  theme_minimal() +
  theme(legend.position = "none") +
  theme(
    panel.border = element_rect(color = "grey60", fill = NA, linewidth = 0.5),
    strip.background = element_rect(fill = "grey95", color = "grey80")
  ) +
  labs(
    title = "Predicted Interest in Politics by Age, Treatment, and Labour Status",
    subtitle = "Model: Interest ~ Treatment * Age * Labour Status",
    x = "Age group",
    y = "Predicted interest (0–10)"
  )

ggsave(
  "figures/Figure E1.png",
  width = 27,
  height = 18,
  units = "cm"
)
```




